package com.mysecurity.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    /**
     * 认证成功后，具体执行的代码
     * @param request
     * @param response
     * @param authentication 认证成功后的用户主体对象
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //判定用户是否认证成功
        if(authentication.isAuthenticated()){
            System.out.println("当前登录的用户是："+authentication.getName());
            //认证成功，重定向到main
            response.sendRedirect("/user/list");
        }
    }
}